/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.inner;

import com.workcell.workdata.im.component.cim.pusher.BroadcastMessagePusher;
import com.workcell.workdata.im.component.cim.pusher.MessagePusherProxy;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.request.InnerMessageRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.service.MessageIndexService;
import com.workcell.workdata.im.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController("InnerMessageController")
@RequestMapping("/inner/message")
@Api(produces = "application/json", tags = "内部消息相关接口" )
public class MessageController {

	@Resource
	private BroadcastMessagePusher broadcastMessagePusher;

	@Resource
	private MessagePusherProxy messagePusherProxy;

	@Resource
	private MessageService messageService;

	@Resource
	private MessageIndexService messageIndexService;

	/**
	 * 开放给其他系统，内部调用发送消息
	 */
	@ApiOperation(httpMethod = "POST", value = "发送消息")
	@PostMapping(value = "", produces = "application/json")
	public ResponseEntity<?> send(@RequestBody InnerMessageRequest request) {
		return messagePusherProxy.push(request.ofMessage(), Origin.UNKNOWN);
	}

	@ApiOperation(httpMethod = "POST", value = "全部成员系统通知")
	@PostMapping(value = "/broadcast")
	public ResponseEntity<Void> broadcast(@RequestBody InnerMessageRequest request) {
		broadcastMessagePusher.push(request.ofMessage(),Origin.UNKNOWN);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "在线成员系统通知")
	@PostMapping(value = "/broadcast/online")
	public ResponseEntity<Void> broadcastOnline(@RequestBody InnerMessageRequest request) {
		broadcastMessagePusher.pushOnline(request.ofMessage());
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除单个消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "消息ID", paramType = "path",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "action", value = "消息类型", paramType = "query",  dataTypeClass = String.class,example = "0")
	})
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> delete(@PathVariable Long id ,@RequestParam String action) {

		messageService.delete(id,action);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除消未接收消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uid", value = "用户ID", paramType = "query",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "action", value = "消息类型", paramType = "query",  dataTypeClass = String.class,example = "0")
	})
	@DeleteMapping(value = "/index")
	public ResponseEntity<Void> deleteIndex(@RequestParam Long uid ,@RequestParam String action) {

		messageIndexService.delete(uid,action);

		return ResponseEntity.ok();
	}
}
